﻿#define HOST

using System;
using System.Collections.Generic;
using System.Text;

using System.Net;
using System.IO;

using FastJSON;

namespace Core
{


    public static class RST
    {
        //const string BaseRSTURI = "http://www.server.com/index.php";
        const string Protocol = "http:";

#if HOST
        const string HOSTNAME = "www.server.com";
#else
        const string HOSTNAME = "127.0.0.1";
#endif
        const string BaseRSTURI = Protocol + "//" + HOSTNAME + "/index.php";

        static readonly Encoding DefaultEncoding = Encoding.UTF8;

        public static string GetRSTString(string Model, string Action, object Query)
        {
            return GetRSTString(Model, Action, FastJSON.JSON.Instance.ToJSON(Query));
        }

        public static string GetRSTString(string Model, string Action, string POST)
        {
            WebRequest wrq = WebRequest.Create(string.Format(string.Format("{0}?m={1}&a={2}", BaseRSTURI, Model, Action)));
            //wrq.Headers.Add(string.Format("Host: {0};", HOSTNAME));
            //wrq.Headers.Add("Connection: keep-alive;");
            wrq.Method = "POST";
            wrq.ContentType = "application/x-www-form-urlencoded;";
            wrq.Method = "POST";
            if (POST != null && POST.Length > 0)
            {
                byte[] buff = DefaultEncoding.GetBytes(POST);
                Stream sw = wrq.GetRequestStream();
                sw.Write(buff, 0, buff.Length);
                sw.Close();
            }
            //-----------------------------------------------------------Credentials-----------------------------------------------------------//
            if (Session.Certificate != null)
            {
                wrq.Headers.Add(string.Format("Cookie: SESSIONID={0};", Session.Certificate.Id));
            }
            //-----------------------------------------------------------Credentials-----------------------------------------------------------//
            WebResponse wrp = wrq.GetResponse();

            string str = new StreamReader(wrp.GetResponseStream()).ReadToEnd();

            File.WriteAllText("Request.json", POST);
            File.WriteAllText("Response.json", str);
            return str;

            return new StreamReader(wrp.GetResponseStream()).ReadToEnd();
















            HttpWebRequest hwrq = HttpWebRequest.Create(string.Format("{0}?m={1}&a={2}", BaseRSTURI, Model, Action)) as HttpWebRequest;
            hwrq.Method = "POST";
            hwrq.ContentType = "application/x-www-form-urlencoded;";
            if (POST != null && POST.Length > 0)
            {
                byte[] buff = DefaultEncoding.GetBytes(POST);
                Stream sw = hwrq.GetRequestStream();
                sw.Write(buff, 0, buff.Length);
                sw.Close();
            }
            else
            {
                StreamWriter sw = new StreamWriter(hwrq.GetRequestStream());
                sw.Write("_");
                sw.Close();
            }
            //-----------------------------------------------------------Credentials-----------------------------------------------------------//

            if (Session.Certificate != null)
            {
                if (hwrq.CookieContainer == null)
                {
                    hwrq.CookieContainer = new CookieContainer();
                }
                hwrq.CookieContainer.SetCookies(new Uri(HOSTNAME), string.Format("SESSIONID:{0}", Session.Certificate.Id));
                hwrq.CookieContainer.Add(new Cookie("SessionId", Session.Certificate.Id, "/", HOSTNAME));
            }

            //-----------------------------------------------------------Credentials-----------------------------------------------------------//

            HttpWebResponse hwrp = hwrq.GetResponse() as HttpWebResponse;

            return new StreamReader(hwrp.GetResponseStream(), DefaultEncoding).ReadToEnd();
        }
    }
}